package com.zlj.huanxin.login.activity;

import android.animation.Animator;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.zlj.huanxin.HuanXinHelper;
import com.zlj.huanxin.MainActivity;
import com.zlj.huanxin.R;
import com.zlj.huanxin.base.BaseInitActivity;
import com.zlj.huanxin.login.activity.LoginActivity;
import com.zlj.huanxin.utils.EaseThreadManager;

public class SplashActivity extends BaseInitActivity {
    private ImageView ivSplash;
    private TextView tvProduct;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initSystemFit() {
        setFitSystemForTheme(false, R.color.transparent);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        ivSplash = findViewById(R.id.iv_splash);
        tvProduct = findViewById(R.id.tv_product);
    }

    @Override
    protected void initData() {
        ivSplash.animate()
                .alpha(1)
                .setDuration(500)
                .setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        loginSDK();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                })
                .start();

        tvProduct.animate()
                .alpha(1)
                .setDuration(500)
                .start();
    }

    private void loginSDK() {
        if (HuanXinHelper.getInstance().getAutoLogin()) {
            if (HuanXinHelper.getInstance().isLoggedIn()) {
                EaseThreadManager.getInstance().runOnIOThread(() -> {
                    loadAllConversationsAndGroups();
                });
                MainActivity.startAction(mContext);
                finish();
            } else {
                LoginActivity.startAction(mContext);
                finish();
            }
        } else {
            LoginActivity.startAction(mContext);
            finish();
        }
    }

    /**
     * 从本地数据库加载所有的对话及群组
     */
    private void loadAllConversationsAndGroups() {
        // 初始化数据库
        initDb();
        // 从本地数据库加载所有的对话及群组
//        getChatManager().loadAllConversations();
//        getGroupManager().loadAllGroups();
    }

    /**
     * init room
     */
    public void initDb() {
//        HuanXinHelper.getInstance(HuanXinApplication.getInstance()).initDb(getCurrentUser());
    }
}